<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Linkslistssetting extends Model
{
    protected $table = 'links_lists_setting';
    protected $primaryKey = 'id';

    public static function getLinksListsSetting($links_id)
    {
        return self::select("links_id","nike","logo","describe","wechat","button_show","button_text","miniapp_style","service_notice","service_image","qrcode_notice","qrcode_switch_type","work_time","device_bind","sph_id","sph_spid","sph_img")->where("links_id",$links_id)->first();
    }

    public static function createLinksListsSetting($links_id,$nike,$logo,$describe,$wechat,$button_show,$button_text,$miniapp_style,$service_notice,$service_image,$qrcode_notice,$device_bind,$work_time,$qrcode_switch_type,$sph_id,$sph_spid,$sph_img)
    {
        return self::insert(["links_id"=>$links_id,"nike"=>$nike,"logo"=>$logo,"describe"=>$describe,"describe"=>$describe,"describe"=>$describe,"wechat"=>$wechat,"button_show"=>$button_show,"button_text"=>$button_text,"miniapp_style"=>$miniapp_style,"service_notice"=>$service_notice,"service_image"=>$service_image,"qrcode_notice"=>$qrcode_notice,"device_bind"=>$device_bind,"work_time"=>$work_time,"qrcode_switch_type"=>$qrcode_switch_type,"sph_id"=>$sph_id,"sph_spid"=>$sph_spid,"sph_img"=>$sph_img,"created_at"=>time()]);
    }

    public static function updateLinksListsSetting($links_id,$nike,$logo,$describe,$wechat,$button_show,$button_text,$miniapp_style,$service_notice,$service_image,$qrcode_notice,$device_bind,$work_time,$qrcode_switch_type,$sph_id,$sph_spid,$sph_img)
    {
        return self::where("links_id",$links_id)->update(["links_id"=>$links_id,"nike"=>$nike,"logo"=>$logo,"describe"=>$describe,"describe"=>$describe,"describe"=>$describe,"wechat"=>$wechat,"button_show"=>$button_show,"button_text"=>$button_text,"miniapp_style"=>$miniapp_style,"service_notice"=>$service_notice,"service_image"=>$service_image,"qrcode_notice"=>$qrcode_notice,"device_bind"=>$device_bind,"work_time"=>$work_time,"qrcode_switch_type"=>$qrcode_switch_type,"sph_id"=>$sph_id,"sph_spid"=>$sph_spid,"sph_img"=>$sph_img]);
    }

    public static function linkSearch($miniapp_style,$keyword)
    {
        $query = self::select("links_lists.id","links_lists.users_id","links_lists.title","links_lists_setting.nike","links_lists_setting.describe","links_lists.disable","links_lists.limit_pv","links_lists.end_time","links_lists.links_id","links_lists.miniapp_status")->leftJoin('links_lists', 'links_lists.links_id', '=', 'links_lists_setting.links_id');
        if ($miniapp_style == 1) {
            $query->orwhere("links_lists_setting.miniapp_style",1);
        } else {
            $query->orwhere("links_lists_setting.miniapp_style",2);
        }
        if (!empty($keyword)) {
            $query->orwhere("links_lists_setting.nike","like","%".$keyword."%")->orwhere("links_lists_setting.describe","like","%".$keyword."%")->orwhere("links_lists.title","like","%".$keyword."%");
        }
        return $query->paginate(20);

    }
}
